package com.kaifamiao.contraller;

import com.kaifamiao.model.OrderModel;
import com.kaifamiao.model.SelectSeatModel;
import com.kaifamiao.model.UserModel;
import com.kaifamiao.service.OrderService;
import com.kaifamiao.service.SelectSeatService;
import com.kaifamiao.service.UserService;
import com.kaifamiao.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class SelectSeatController {

    @Autowired
    SelectSeatService service;

    //存储订座信息
    @Autowired
    OrderService orderService;

    //存储用户信息
    @Autowired
    UserService userService;

    //根据楼层，教室，座位定位查询
    @RequestMapping("/selectOneSeat")
//    public ResponseUtil selectSeat(@RequestParam(value = "sno",required = false) int sno){
    public ResponseUtil selectOneSeat(){

        List<SelectSeatModel> list = service.selectOneSeat(2,2,"02");
        if(list.size() == 0){
            return ResponseUtil.error("请求失败");
        }else{
            return ResponseUtil.ok("请求成功",list);
        }

    }

    //根据楼层，教室定位查询出一个教室里座位的状态
    @RequestMapping("/selectSeat")
    @CrossOrigin("*")
//    public ResponseUtil selectSeat(@RequestParam(value = "fno",required = false) int fno,
//                                   @RequestParam(value = "rno",required = false) String rno){
    public ResponseUtil selectSeat(){

        List<SelectSeatModel> list = service.selectSeat(1,"01");

        if(list.size() == 0){
            return ResponseUtil.error("请求失败");
        }else{
            return ResponseUtil.ok("请求成功",list);
        }

    }


    //根据楼层，教室定位查询出所有已选座位的座位号
    @RequestMapping("/oKSeat")
    @CrossOrigin("*")
    public ResponseUtil oKSeat(@RequestParam(value = "fno",required = false) int fno,
                                   @RequestParam(value = "rno",required = false) String rno){
//    public ResponseUtil oKSeat(){
        List<Integer> list = service.oKSeat(fno,rno);

        if(list.size() == 0){
            return ResponseUtil.error("请求失败");
        }else{
            return ResponseUtil.ok("请求成功",list);
        }

    }

    //订座操作
    @RequestMapping("/orderSeat")
    @CrossOrigin("*")
    public ResponseUtil orderSeat(@RequestParam(value = "sno",required = false) int sno,
                                   @RequestParam(value = "fno",required = false) int fno,
                                   @RequestParam(value = "rno",required = false) String rno,
                                   @RequestParam(value = "id",required = false) Long id){

        //1.将订座信息存入订座信息表
        int add = orderService.insertOrder(fno,rno,sno);
//        int add = OrderService.insertOrder(1,"01",2);

        //2.拿到订座信息的OId
        int oid = orderService.selectOId(fno,rno,sno);
//        int Oid = OrderService.selectOId(1, "01", 2);

        //3.将OId更新给选中该座位的用户
        int k = userService.addOid(oid, id.intValue());
//        int k = userService.addOid(Oid,5);

        //4.改变选中座位状态
        int change = service.updateSeat(sno,fno,rno);
//        int change = service.updateSeat(2,1,"01");


        if(change == 0 || add == 0 || k == 0){
            return ResponseUtil.error("请求失败");
        }else{
            return ResponseUtil.ok("请求成功",oid);
        }

    }

    //退座操作
    @RequestMapping("/resetSeat")
    @CrossOrigin("*")
    public ResponseUtil orderSeat(@RequestParam(value = "id",required = false) Long id,
                                  @RequestParam(value = "oid",required = false) Integer oid,
                                  @RequestParam(value = "sno",required = false) int sno,
                                  @RequestParam(value = "fno",required = false) int fno,
                                  @RequestParam(value = "rno",required = false) String rno

    ){

        //1.将用户的 oid 重置为 null
        int k = userService.setOidNull(id.intValue());

        //2.删除 order表的订座记录
        int s = orderService.deleteOrder(oid);

        //3.将座位状态重新置为 0
        int a = service.resetSeat(sno, fno, rno);

        if(k == 0 || s == 0 || a == 0){
            return ResponseUtil.error("请求失败");
        }else{
            return ResponseUtil.ok("请求成功");
        }
    }

}
